package tq.com.tqteacher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.security.PublicKey;

import tq.com.tqteacher.R;

public class TQInputView extends LinearLayout {
    private TextView mTitle ;
    private EditText mInput;

    public TQInputView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public TQInputView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attributeSet){
        LayoutInflater.from(context).inflate(R.layout.layout_input, this);

        mTitle = this.findViewById(R.id.title);
        mInput = findViewById(R.id.content);

        TypedArray array = context.obtainStyledAttributes(attributeSet, R.styleable.TQInputView);
        String title = array.getString(R.styleable.TQInputView_inputview_title);
        if(title != null){
            mTitle.setText(title);
        }

        float weight = array.getFloat(R.styleable.TQInputView_inputview_titleWidht, 0.4f);
        LinearLayout.LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, weight);
        params.gravity = Gravity.CENTER_VERTICAL;
        mTitle.setLayoutParams(params);

        LinearLayout.LayoutParams editParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, (1 - weight));
        mInput.setLayoutParams(editParams);
        array.recycle();
    }


    public String  getText(){
        return mInput.getText().toString();
    }

    public void setText(String text){
        mInput.setText(text);
    }
}
